package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.Goods;
import school.domain.Shop;
import school.mapper.GoodsMapper;
import school.mapper.ShopMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.GoodsPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RequestMapping("goods")
@Controller
public class GoodsController extends BaseController {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 列表
     *
     * @param goodsPo
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(GoodsPo goodsPo, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        if (goodsPo.getName() != null) {
            criteria.andCondition("g.name like '%" + goodsPo.getName() + "%'");
        }

        if (goodsPo.getNo() != null) {
            criteria.andCondition("g.no like '%" + goodsPo.getNo() + "%'");
        }

        if (goodsPo.getShopname() != null) {
            criteria.andCondition("s.name like '%" + goodsPo.getShopname() + "%'");
        }

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<GoodsPo> pageInfo = new PageInfo<>(goodsMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "goods/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        model.addAttribute(goods);
        List<Shop> shopList = shopMapper.selectAll();
        model.addAttribute("shopList", shopList);
        return "goods/detail";
    }

    /**
     * 修改信息
     *
     * @param goods
     * @return
     */
    @RequestMapping("update")
    public String update(Goods goods, MultipartFile file) {
        if (!file.isEmpty()) {
            String path = saveFile(file);
            goods.setPath(path);
        }
        goodsMapper.updateByPrimaryKeySelective(goods);
        return refresh();
    }

    @GetMapping("add")
    public String add(Model model) {
        List<Shop> shopList = shopMapper.selectAll();
        model.addAttribute("shopList", shopList);
        return "goods/add";
    }

    @PostMapping("add")
    public String add(Goods goods, MultipartFile file) {
        if (!file.isEmpty()) {
            String path = saveFile(file);
            goods.setPath(path);
        }
        goods.setAddtime(new Date());
        goodsMapper.insertSelective(goods);
        return "redirect:list";
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        goodsMapper.deleteByPrimaryKey(id);
        return refresh();
    }



}
